//
//  XHMessageViewController.m
//  xinhua
//
//  Created by fengyn on 15/10/29.
//  Copyright © 2015年 lexingsoft. All rights reserved.
//

#import "XHMessageViewController.h"

#import "XHMessageCell.h"

#import "Services.h"

#import <SVPullToRefresh/SVPullToRefresh.h>

static const NSUInteger kPageSize = 20;
static const NSUInteger kPageInit = 0;

@interface XHMessageViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSMutableArray *m_messagesArray;

@property (assign, nonatomic) NSUInteger page;
@property (assign, nonatomic) BOOL hasNextPage;
@end

@implementation XHMessageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.titleView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shouye_img_xd"]];
    
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHMessageCell" bundle:nil] forCellReuseIdentifier:XHMessageCellIdentifier];
    [PublicMethod setExtraCellLineHidden:self.ui_tableView];
    [self.ui_tableView setBackgroundColor:[ColorManager colorWithHexString:@"#F4F4F4"]];
//
    self.page=kPageInit;
    [self.ui_tableView addInfiniteScrollingWithActionHandler:^{
        if (!self.hasNextPage)
        {
            //[PromptAction showErrorPrompt:@"暂无更多数据"];
        }
        if (self.m_messagesArray.count>=kPageSize){
            [self loadDataWithPage:self.page+1];
            return;
        }
        [self.ui_tableView.infiniteScrollingView stopAnimating];
    }];
    [self.ui_tableView addPullToRefreshWithActionHandler:^{
        self.page=kPageInit;
        [self loadDataWithPage:self.page];
    }];
    [PublicMethod setPullToRefreshView:self.ui_tableView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!self.m_messagesArray)
    {
        self.page=kPageInit;
        [self loadDataWithPage:self.page];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)loadDataWithPage:(NSUInteger)page
{
    [ServiceManager getAllMessagesWithPageIndex:page success:^(NSDictionary *data)
    {
        if (page==kPageInit||self.m_messagesArray==nil)
        {
            self.m_messagesArray=[NSMutableArray array];
        }
        NSArray * datas=data[@"result"];
        self.hasNextPage=(datas.count==kPageSize);
        if (datas.count>0)
        {
            self.page=page;
            [self.m_messagesArray addObjectsFromArray:datas];
        }
        [self.ui_tableView reloadData];
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [self.ui_tableView.pullToRefreshView stopAnimating];
        [self setNothingViewIsHide:self.m_messagesArray.count!=0];
    } failure:^(NSDictionary *data) {
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [self.ui_tableView.pullToRefreshView stopAnimating];
        [self setNothingViewIsHide:self.m_messagesArray.count!=0];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=0;
    rows=self.m_messagesArray.count;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell=[self configOtherCellWithIndexPath:indexPath];
    return cell;
}

-(XHMessageCell *)configOtherCellWithIndexPath:(NSIndexPath *)indexPath
{
    XHMessageCell *cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHMessageCellIdentifier];
    NSDictionary *dic=self.m_messagesArray[indexPath.row];
    cell.ui_titleLabel.text=dic[@"messageTitle"];
    cell.ui_timeLabel.text=[[NSDate dateWithString:dic[@"recDate"]] stringDateYear];
    [cell setBackgroundColor:[ColorManager colorWithCellBackground]];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=0;
    XHMessageCell * cell = (XHMessageCell*)[self tableView:self.ui_tableView cellForRowAtIndexPath:indexPath];
    height = [cell cellHeight];

    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 10;
    return height;
}


@end
